<template>
	<a-card :bordered="false">
		<SearchForm
			:form-state="searchFormState"
			:form-fields="[
				{ label: '名称关键词', name: 'searchKey', span: 8 },
				{ label: '存储位置', name: 'engine', span: 8 }
			]"
			@search="table.refresh(true)"
			@reset="handleReset"
			ref="searchFormRef"
		>
			<template #searchKey="{ formState }">
				<a-input v-model:value="formState.searchKey" placeholder="请输入文档名称关键词"></a-input>
			</template>
			<template #engine="{ formState }">
				<a-select v-model:value="formState.engine" placeholder="请选择存储位置" :options="engineOptions"></a-select>
			</template>
		</SearchForm>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:expand-row-by-click="true"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:row-selection="options.rowSelection"
			:operator-config="operatorConfig"
		>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'thumbnail'">
					<img :src="record.thumbnail" class="record-img" v-if="record.suffix === 'png' || record.suffix === 'jpg' || record.suffix === 'jpng' || record.suffix === 'ico' || record.suffix === 'gif'" />
					<img src="/src/assets/images/fileImg/docx.png" class="record-img" v-else-if="record.suffix === 'doc' || record.suffix === 'docx'" />
					<img src="/src/assets/images/fileImg/xlsx.png" class="record-img" v-else-if="record.suffix === 'xls' || record.suffix === 'xlsx'" />
					<img src="/src/assets/images/fileImg/zip.png" class="record-img" v-else-if="record.suffix === 'zip'" />
					<img src="/src/assets/images/fileImg/rar.png" class="record-img" v-else-if="record.suffix === 'rar'" />
					<img src="/src/assets/images/fileImg/ppt.png" class="record-img" v-else-if="record.suffix === 'ppt' || record.suffix === 'pptx'" />
					<img src="/src/assets/images/fileImg/txt.png" class="record-img" v-else-if="record.suffix === 'txt'" />
					<img src="/src/assets/images/fileImg/html.png" class="record-img" v-else-if="record.suffix === 'html'" />
					<img src="/src/assets/images/fileImg/file.png" class="record-img" v-else />
				</template>
				<template v-if="column.dataIndex === 'engine'">
					{{ $TOOL.dictTypeData('FILE_ENGINE', record.engine) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a @click="detailRef.onOpen(record)">详情</a>
					<a-divider type="vertical" />
					<a :href="record.downloadPath" target="_blank">下载</a>
					<a-divider type="vertical" />
					<a-popconfirm title="删除此文档？" @confirm="deleteFile(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<uploadForm ref="uploadForm" @successful="table.refresh(true)" />
	<detail ref="detailRef" />
</template>
<script setup name="devFileIndex">
	import { message } from 'ant-design-vue'
	import fileApi from '@/api/dev/fileApi'
	import { tool } from 'elead-ui-tool-v3'
	import UploadForm from '@/pagesDefault/dev/file/uploadForm.vue'
	import detail from '@/pagesDefault/dev/file/detail.vue'
	// 定义tableDOM
	const table = ref()
	const form = ref()
	const searchFormRef = ref()
	let searchFormState = reactive({})
	const uploadForm = ref()
	const detailRef = ref()
	const columns = [
		{
			title: '文档名称',
			dataIndex: 'name',
			ellipsis: true,
			width: 280
		},
		{
			title: '缩略图',
			dataIndex: 'thumbnail',
			ellipsis: true,
			width: 80
		},
		{
			title: '文档大小',
			dataIndex: 'sizeInfo',
			ellipsis: true
		},
		{
			title: '文档后缀',
			dataIndex: 'suffix',
			ellipsis: true
		},
		{
			title: '储存引擎',
			dataIndex: 'engine',
			ellipsis: true
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 180
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		return fileApi.filePage(Object.assign(parameter, searchFormState)).then((data) => {
			return data
		})
	}
	// 删除
	const deleteFile = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		fileApi.fileDelete(params).then(() => {
			table.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchFile = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		fileApi.fileDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
	// 存储位置
	const engineOptions = tool.dictTypeList('FILE_ENGINE').map((item) => {
		return {
			value: item['dictValue'],
			label: item['name']
		}
	})

	// 重置表单
	const handleReset = () => {
		searchFormRef.value?.resetFields()
	}

	// 操作按钮配置
	const operatorConfig = computed(() => [
		{
			text: '文档上传',
			icon: 'fa fa-upload',
			onClick: () => uploadForm.value.openUpload()
		},
		{
			text: '删除',
			icon: 'fa fa-trash',
			danger: true,
			onClick: deleteBatchFile,
			disabled: selectedRowKeys.value.length === 0
		}
	])
</script>
<style scoped>
	.record-img {
		width: 40px;
		height: 40px;
	}
	.ant-form-item {
		margin-bottom: 0 !important;
	}
	.snowy-buttom-left {
		margin-left: 8px;
	}
</style>
